package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.Worker.WorkertType;
import com.sand.dto.Select.UserSelect;
import com.sand.dto.worker.AddWorker;
import com.sand.dto.worker.WorkerDto;
import com.sand.entity.user.Owner;
import com.sand.entity.user.User;
import com.sand.entity.user.worker;
import com.sand.feign.UserFeign;
import com.sand.mapper.WorkerMapper;
import com.sand.owner.Intentionality;
import com.sand.result.AppHttpCodeEnum;
import com.sand.result.ResponseResult;
import com.sand.service.WorkerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WorkerServiceImpl extends ServiceImpl<WorkerMapper , worker> implements WorkerService {

    @Autowired
    private UserFeign userFeign;


    /**
     * 新增
     *
     * @param addOrWorker
     * @return
     */
    @Override
    public String add(AddWorker addWorker) {
        //获取id
        Integer userid = addWorker.getUserid();

        //获取意向度
        Integer workertype = addWorker.getWorkertype();

        if (userid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入用户id");
        }

        //根据id查询用户
        User user = userFeign.getById(userid).getData();


        if (workertype == null){
            //没有传入工人类型默认全工
            addWorker.setWorkertype(WorkertType.QUAN);
        }

        if (user == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"不存在该用户");
        }

        //保存
        worker worker = new worker();

        worker.setUserid(userid);
        worker.setType(workertype);

        save(worker);

        return "新增工人成功";
    }



    /**
     * 查询工人
     *
     * @param userSelect
     * @return
     */
    @Override
    public WorkerDto select(UserSelect userSelect) {
        Integer id = userSelect.getId();

        String phone = userSelect.getPhone();

        //两个都没传入
        if (id == null && phone.isEmpty()){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"未传入要查寻的用户id或号码");
        }

        //两个都传入
        if (id != null && !phone.isEmpty()) {

            //根据工人表id查询
            worker worker = getById(id);
            if (worker == null){
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该工人不存在");
            }

            //根据电话号码获取人员信息
            ResponseResult<User> inquire = userFeign.inquire(new UserSelect(null, phone));
            if (inquire.getData() == null) {
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该电话号码不存在");
            }

            return getWorkerDto(inquire, worker);

        }

        //只传入id
        if (id != null){
            //根据工人表id查询
            worker worker = getById(id);
            if (worker == null){
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该工人不存在");
            }

            //获得工人的userid
            Integer userid = worker.getUserid();

            // 根据userid查询工人基本信息
            ResponseResult<User> inquire = userFeign.inquire(new UserSelect(userid, null));

            //拼接
            return getWorkerDto(inquire, worker);
        }

        //只传了电话号码
        ResponseResult<User> inquire = userFeign.inquire(new UserSelect(null, phone));

        //根据电话号码获取人员信息
        if (inquire.getData() == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该电话号码不存在");
        }

        //获取人员信息中的id
        Integer Uid = inquire.getData().getId();

        //根据id找到工人
        worker worker = getOne(new LambdaQueryWrapper<worker>().eq(com.sand.entity.user.worker::getUserid, Uid));

        //拼接
        return getWorkerDto(inquire, worker);
    }


    /**
     * 用于拼接工人和人员信息
     * @param inquire
     * @return
     */
    private static WorkerDto getWorkerDto(ResponseResult<User> inquire, worker worker) {
        WorkerDto workerDto = new WorkerDto();

        BeanUtils.copyProperties(inquire.getData(),workerDto);
        BeanUtils.copyProperties(worker,workerDto);

        return workerDto;
    }


}
